import { createRouter, createWebHistory,useRouter, createWebHashHistory } from 'vue-router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import {useStore} from "vuex";
import axios from 'axios'

const routes = [
  {
    path:'/',
    redirect:'/homePage',
    component:()=>import('@/views/LoginPage/index.vue'),
  },{
    path:'/first',
    name:'first',
    // redirect:'/login',
    component:()=>import('@/views/FirstPage/index.vue'),
    children: [{
      path:'/login',
      name:'login',
      component:()=>import('@/views/LoginPage/index.vue'),
    },{
      path:'/register',
      name:'register',
      component:()=>import('@/views/Register/index.vue'),
    },]
  },
  {
    path:'/homePage',
    name:'homePage',
    component:()=>import('@/views/HomePage/index.vue'),
    redirect: 'bench',
    children:[
      {
        path:'/empty',
        name:'empty',
        component:()=>import('@/views/HomePage/Empty.vue'),
      },{
        path:'/fileList',
        name:'fileList',
        component:()=>import('@/views/FilePage/index.vue'),
      },{
        path:'/flow',
        name:'flow',
        component:()=>import('@/views/FlowPage/index.vue'),
        redirect:'/users',
        children:[
          {
            path:'/manage',
            name:'manage',
            component:()=>import('@/components/UserManage/Manage.vue')
          },{
            path:'/users',
            name:'users',
            component:()=>import('@/components/UserManage/Users.vue')
          }
        ]
      },{
        path:'/user',
        name:'user',
        component:()=>import('@/views/UserPage/index.vue'),
      },{
        path:'/admin',
        component:()=>import('@/views/AdminHelper/index.vue'),
      },{
        path:'/help',
        component:()=>import('@/views/UserHelper/index.vue'),
      },{
        path:'/echarts',
        component:()=>import('@/views/Echarts/index.vue'),
      },{
        path:'/rule',
        name:'rule',
        redirect:'/rules',
        component:()=>import('@/views/Rules/index.vue'),
        children:[
          {
            path:'/rules',
            component:()=>import('@/views/Rules/Rules.vue'),
          }
        ]
      },{
        path:'/OA',
        name:'OA',
        component:()=>import('@/views/OA/index.vue'),
        children:[
          {
            path:'/OA/leave',
            name:'leave',
            children:[
              {
                path:'/OA/leave/my-leave',
                name:'my-leave',
                children:[
                  {
                    path:'/OA/my-leave/all',
                    component:()=>import('@/views/OA/Leave/MyLeaveApprove/index.vue'),
                    children:[
                      {
                        path:'/OA/my-leave/going',
                        component:()=>import('@/views/OA/Leave/MyLeaveApprove/GoingFlow.vue'),
                      },{
                        path:'/OA/my-leave/passed',
                        component:()=>import('@/views/OA/Leave/MyLeaveApprove/PassedFlow.vue'),
                      },{
                        path:'/OA/my-leave/refuse',
                        component:()=>import('@/views/OA/Leave/MyLeaveApprove/RefusedFlow.vue'),
                      },{
                        path:'/OA/my-leave/list',
                        component:()=>import('@/views/OA/Leave/MyLeaveApprove/All.vue'),
                      }
                    ]
                  }
                ]
              },
              {
                path:'/leave/my-handle',
                name:'/leave/my-handle',
                children: [
                    {
                      path:'/OA/my-handled',
                      component:()=>import('@/views/OA/Leave/MyHandled/index.vue'),
                      children:[
                        {
                          path:'/OA/my-handled/to-handle',
                          component:()=>import('@/views/OA/Leave/MyHandled/Handle.vue'),
                        },{
                          path:'/OA/my-handled/handled',
                          component:()=>import('@/views/OA/Leave/MyHandled/Handled.vue'),
                        },{
                          path:'/OA/my-handled/all',
                          component:()=>import('@/views/OA/Leave/MyHandled/All.vue'),
                        }
                      ]
                    },
                  ]
              },
              {
                path:'/leave/my-copy',
                name:'/leave/my-copy',
                children: [
                  {
                    path:'/OA/my-copy',
                    name:'all-handle',
                    component:()=>import('@/views/OA/Leave/Copy/Copy.vue'),
                  },
                ]
              }
            ]
          },
          {
            path:'/OA/seal',
            name:'seal',
            children: [
              {
                path:'/OA/seal/my-seal',
                component:()=>import("@/views/OA/Seal/MyApprove/index.vue"),
                children: [
                  {
                    path:'/OA/seal/all',
                    component:()=>import("@/views/OA/Seal/MyApprove/components/All.vue"),
                  },{
                    path:'/OA/seal/going',
                    component:()=>import("@/views/OA/Seal/MyApprove/components/Going.vue"),
                  },{
                    path:'/OA/seal/allowed',
                    component:()=>import("@/views/OA/Seal/MyApprove/components/Allowed.vue"),
                  },{
                    path:'/OA/seal/rejected',
                    component:()=>import("@/views/OA/Seal/MyApprove/components/Rejected.vue"),
                  },{
                    path:'/OA/seal/toEdit',
                    component:()=>import("@/components/OA/Seal/MyApprove/components/ToEdit.vue"),
                  },
                ]
              },
              {
                path:'/OA/seal/usage-log',
                component:()=>import("@/views/OA/Seal/SealUsageLog/index.vue")
              },
              {
                path:'/OA/seal/handle',
                component:()=>import("@/views/OA/Seal/MyHandle/index.vue"),
                children: [
                  {
                    path:'/OA/seal/handle/all',
                    component:()=>import("@/views/OA/Seal/MyHandle/Components/All.vue"),
                  },{
                    path:'/OA/seal/handle/going',
                    component:()=>import("@/views/OA/Seal/MyHandle/Components/Going.vue"),
                  },{
                    path:'/OA/seal/handle/handled',
                    component:()=>import("@/views/OA/Seal/MyHandle/Components/Handled.vue"),
                  },
                ]
              },
              {
                path:'/OA/seal/copy',
                component:()=>import("@/views/OA/Seal/MyCopy/index.vue")
              }
            ]
          },
          {
            path:'/app-mine',
            name:'app-mine',
            component:()=>import('@/views/OA/Approve/Mine/index.vue'),
            children: [
              {
                path: '/mine-all',
                name:'mine-all',
                component:()=>import("@/views/OA/Approve/Mine/AllApp/index.vue")
              },
              {
                path: '/mine-complete',
                name:'complete-all',
                component:()=>import("@/views/OA/Approve/Mine/CompleteApp/index.vue")
              },
              {
                path: '/mine-going',
                name:'going-all',
                component:()=>import("@/views/OA/Approve/Mine/GoingApp/index.vue")
              },
            ],
          },
          {
            path:'/app-handle',
            name:'app-handle',
            component:()=>import('@/views/OA/Approve/MyHandle/index.vue'),
            children: [
              {
                path:'/app-handle-all',
                name:'app-handle-all',
                component:()=>import("@/views/OA/Approve/MyHandle/HandleList/index.vue")
              },
              {
                path:'/app-handle-handled',
                name:'app-handled-all',
                component:()=>import("@/views/OA/Approve/MyHandle/Handled/index.vue")
              },
              {
                path:'/app-handle-toHandle',
                name:'app-toHandle-all',
                component:()=>import("@/views/OA/Approve/MyHandle/ToHandle/index.vue")
              },
            ],
          },
          {
            path:'/app-copy',
            name:'app-copy',
            component:()=>import('@/views/OA/Approve/Copy/index.vue'),
          },
        ]
      },{
        path:'/bench',
        name:'bench',
        component:()=>import('@/views/WorkBench/index.vue'),
      },{
        path:'/msg',
        name:'msg',
        component:()=>import('@/views/Message/index.vue'),
      },{
        path:'/finance',
        name:'finance',
        redirect:'all_bill',
        children:[
          {
            path:'/all_bill',
            name:'all_bill',
            component:()=>import("@/views/Finance/Bill/All.vue")
          },{
            path:'/handle_bill',
            name:'handle_bill',
            component:()=>import("@/views/Finance/Bill/AllHandleBill.vue"),
            redirect:'fin-to-handle',
            children:[
              {
                path:'/fin-to-handle',
                name:'fin-to-handle',
                component:() => import('@/components/Finance/Bill/ToHandleBill.vue')
              },{
                path:'/fin-handled',
                name:'fin-handled',
                component:() => import('@/components/Finance/Bill/HandledBill.vue')
              },
            ]
          },{
            path:'/copy_bill',
            name:'copy_bill',
            component:()=>import("@/views/Finance/Bill/CopyFinance.vue")
          },{
            path:'/all_invoice',
            name:'all_invoice',
            component:()=>import("@/views/Finance/Bill/CopyFinance.vue")
          },{
            path:'/handle_invoice',
            name:'handle_invoice',
            component:()=>import("@/views/Finance/Bill/CopyFinance.vue")
          },{
            path:'/copy_invoice',
            name:'copy_invoice',
            component:()=>import("@/views/Finance/Bill/CopyFinance.vue")
          },{
            path:'/pro',
            name:'pro',
            component:()=>import('@/views/Project/index.vue'),
          },
        ]
      }, {
        path:'/study',
        name:'study',
        component:()=>import('@/views/Study/index.vue'),
      },{
        path:'/borrow',
        name:'borrow',
        component:()=>import('@/views/Borrow/index.vue'),
        children:[
          {
            path:'/borrow-mine',
            name:'borrow-mine',
            component:()=>import('@/views/Borrow/Mine/index.vue'),
            children:[
              {
                path:'/borrow-mine-all',
                name:'borrow-mine-all',
                component:()=>import('@/views/Borrow/Mine/All/index.vue'),
              },{
                path:'/borrow-mine-complete',
                name:'borrow-mine-complete',
                component:()=>import('@/views/Borrow/Mine/Complete/index.vue'),
              },{
                path:'/borrow-mine-going',
                name:'borrow-mine-going',
                component:()=>import('@/views/Borrow/Mine/Going/index.vue'),
              }
            ]
          },
          {
            path:'/borrow-copies',
            name:'borrow-copy',
            component:()=>import('@/views/Borrow/Copy/index.vue'),
            children: [
              {
                path:'/borrow-copy',
                name:'borrow-copy',
                component:()=>import('@/views/Borrow/Copy/Borrow/index.vue'),
              },
              {
                path:'/back-copy',
                name:'back-copy',
                component:()=>import('@/views/Borrow/Copy/Back/index.vue'),
              },
            ],
          },
          {
            path:'/borrow-handle',
            name:'borrow-handle',
            component:()=>import('@/views/Borrow/MyHandle/index.vue'),
            children: [
              {
                path:'/borrow-handle-complete',
                name:'borrow-handle-complete',
                component:()=>import('@/views/Borrow/MyHandle/Handled/index.vue'),
              },
              {
                path:'/borrow-handle-all',
                name:'borrow-handle-all',
                component:()=>import('@/views/Borrow/MyHandle/HandleList/index.vue'),
              },
              {
                path:'/borrow-handle-todo',
                name:'borrow-handle-todo',
                component:()=>import('@/views/Borrow/MyHandle/ToHandle/index.vue'),
              },
            ]
          }
        ]
      }
    ]
  },
  {
    path:'/previewR',
    component:()=>import('@/views/Rules/RuleFileDetails.vue'),
  },
  {
    path:'/previewS',
    component:()=>import('@/views/OA/Seal/PreviewS.vue'),
  },
  {
    path:'/previewSt',
    component:()=>import('@/views/Study/preview.vue'),
  },
  {
    path:'/previewBO',
    component:()=>import('@/views/Borrow/previewBO.vue'),
  },
  {
    path:'/previewBA',
    component:()=>import('@/views/Borrow/previewBA.vue'),
  },
]

const router = createRouter({
  // history: createWebHashHistory(process.env.BASE_URL),
  history: createWebHistory(process.env.BASE_URL),
  routes
})

router.beforeEach((to,from,next) => {
  NProgress.start()
  console.log()
  if (to.path !== "/login" && !localStorage.getItem('syhyDocManSys')) {
    // 用户没有登录且不在登录页面，重定向到登录页面
    next('/login');
  } else  {
    if (localStorage.getItem('syhyDocManSys')) {
      // 如果有用户信息，保存信息到vuex
      const u = JSON.parse(localStorage.getItem('syhyDocManSys'))
      const store = useStore()
      axios.defaults.headers.common['access-token'] = u.token
      store.state.userInfo.id = u.id
      store.state.userInfo.token = u.token
      store.state.userInfo.username = u.username
    }

    // 用户已经登录或者在登录页面，允许用户继续导航
    next()
  }
})
router.afterEach(() => {
  NProgress.done()
})


export default router
